﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Marmat.Foundation;
using Marmat.Forms;
using System.Linq.Expressions;

namespace Marmat.Template.WinForm
{

    public partial class SearchControl : System.Windows.Forms.UserControl, ISearchControl
    {
        public SearchControl()
        {
            InitializeComponent();
            this.Filter = new SearchControlFilterCollection(this);
            this.Value = new SearchControlValueCollection(this);
        }
        public event EventHandler ValueChanged;


        public ISearchControlFilterCollection Filter { get; }

        public ISearchControlValueCollection Value { get; }

        private void SearchControl1_ValueChanged(object sender, EventArgs e)
        {
            this.ValueChanged?.Invoke(this, e);
        }

        private void SearchWidget_SizeChanged(object sender, EventArgs e)
        {
            this.searchControl1.Left = 574;
            this.searchControl1.Width = this.Width - this.searchControl1.Left - 6;
            this.searchControl1.Top = (this.Height - this.searchControl1.Height) / 2;
        }

        public void AddField(string name, string caption, Type dataType, string condition = null)
        {
            this.searchControl1.Columns.Add(new Forms.SearchControl.Field() { Name = name, Caption = caption, DataType = dataType, Condition = condition });
        }

        public void AddGroup(string name, string caption, string condition)
        {
            this.searchControl1.Columns.Add(new Forms.SearchControl.Group() { Name = name, Caption = caption, Domain = condition });
        }

        public void RemoveFilter(string name)
        {

        }

        public void AddFilter<TEntity>(params string[] columns)
        {
            this.searchControl1.AddCondition<TEntity>(columns);
        }

        public void AddFilter<TEntity>(Expression<Func<TEntity, object>> expression)
        {
            this.searchControl1.AddCondition(expression);
        }
    }
}
